package com.lp.user.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lp.model.config.AppResult;
import com.lp.model.config.CodeMsg;
import com.lp.model.constants.MQConstants;
import com.lp.model.dto.article.PageDto;
import com.lp.model.dto.user.FollowDto;
import com.lp.model.entity.article.vo.PageVO;
import com.lp.model.entity.user.User;
import com.lp.model.exception.ApplicationException;
import com.lp.model.utils.PageUtils;
import com.lp.user.mapper.UserFollowMapper;
import com.lp.user.mapper.UserMapper;
import com.lp.user.mapper.UserinfoMapper;
import com.lp.user.service.IRelationService;
import com.sun.xml.internal.ws.developer.Serialization;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Slf4j
@Service
public class RelationService implements IRelationService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageVO<User> getFansList(PageDto page) {
        //1.计算分页数据
        int limit = page.getPageSize();
        int offset = (page.getCurPage() - 1) * page.getPageSize();
        //2.获取该用户的粉丝 id 列表
        List<Long> fansIdList = userFollowMapper.selectFansListByFollowedId(page.getId(), limit, offset);
        //3.根据 id 列表获取所有粉丝身份信息列表
        List<User> fansList = getUserListByIdList(fansIdList);
        //4.获取粉总数
        Long total = userFollowMapper.selectFansTotalByFollowedId(page.getId());
        //5.返回分页响应
        return PageUtils.getPageVO(fansList, total);
    }

    @Override
    public PageVO<User> getFollowList(PageDto page) {
        //1.计算分页数据
        int limit = page.getPageSize();
        int offset = (page.getCurPage() - 1) * page.getPageSize();
        //2.获取改用户的关注 id 列表
        List<Long> followIdList = userFollowMapper.selectFollowListByFollowedId(page.getId(), limit, offset);
        //3.根据 id 列表获取所有被关注者的身份信息列表
        List<User> followList = getUserListByIdList(followIdList);
        //4.获取关注总数
        Long total = userFollowMapper.selectFollowTotalByFollowerId(page.getId());
        //5.返回分页数据
        return PageUtils.getPageVO(followList, total);
    }

    @Override
    public void follow(FollowDto followDto) {
        //1.非空校验
        if(followDto == null || followDto.getFollowedId() == null ||
                followDto.getFollowerId() == null) {
            log.info(CodeMsg.FAIL_NULL_FOLLOW.toString());
            throw new ApplicationException(AppResult.fail(CodeMsg.FAIL_NULL_FOLLOW));
        }
        //2.给 mq 发送关注消息
        mqSendFollowMsg(followDto);
    }

    /**
     * 通过 mq 发送关注消息
     * @param followDto
     */
    @SneakyThrows
    private void mqSendFollowMsg(FollowDto followDto) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("followerId", followDto.getFollowerId());
        map.put("followedId", followDto.getFollowedId());
        String msg = objectMapper.writeValueAsString(map);
        rabbitTemplate.convertAndSend(MQConstants.USER_EXCHANGE_DIRECT,
                MQConstants.USER_FOLLOW_QUEUE, msg);
    }

    /**
     * 根据用户 id 列表获取用户身份信息列表
     * @param userIdList 粉丝 id 列表
     */
    private List<User> getUserListByIdList(List<Long> userIdList) {
        List<User> userList = new ArrayList<>();
        for (Long fansId : userIdList) {
            User user = userMapper.selectUserById(fansId);
            //脱敏
            user.setPassword("");
            userList.add(user);
        }
        return userList;
    }

}
